"""Build FUSE client"""


def configure_lustre(denv):
    """Do Lustre configure checks"""
    if GetOption('help') or GetOption('clean'):
        return denv

    def _print(msg):
        if not GetOption('silent'):
            print(msg)

    # If Lustre installed build a Lustre-aware libduns
    conf = Configure(denv)
    gotversion = False
    if not conf.CheckLibWithHeader('lustreapi', 'linux/lustre/lustre_user.h',
                                   'c'):
        _print("No installed Lustre version detected")
    else:
        _print("Installed Lustre version detected")
        if not conf.CheckFunc('llapi_unlink_foreign'):
            _print("Lustre version is not compatible")
        else:
            _print("Lustre version is compatible")
            gotversion = True
    if gotversion is True:
        _print("Building with Lustre bindings.")
        denv.AppendUnique(CCFLAGS=['-DLUSTRE_INCLUDE'])
    else:
        _print("Not building with Lustre bindings.")
    return conf.Finish()


def scons():
    """Execute build"""
    Import('env')

    env.d_add_build_rpath()

    denv = env.Clone()

    configure_lustre(denv)

    libraries = ['daos_common', 'daos', 'uuid', 'gurt']

    dfs_src = ['common.c', 'cont.c', 'dir.c', 'file.c', 'io.c', 'lookup.c', 'mnt.c', 'obj.c',
               'pipeline.c', 'readdir.c', 'rename.c', 'xattr.c', 'dfs_sys.c']
    dfs = denv.d_library('dfs', dfs_src, LIBS=libraries)
    denv.Install('$PREFIX/lib64/', dfs)

    libraries = libraries + ['dfs']

    denv.AppendUnique(LIBPATH=[Dir('.')])

    duns = denv.d_library('duns', 'duns.c', LIBS=libraries)
    denv.Install('$PREFIX/lib64/', duns)


if __name__ == "SCons.Script":
    scons()
